'use strict';

var model = require('../../model/user.js');
var debug = require('debug')('dao_user_impl');
var appUtils = require('../../common/app_utils.js');
var errInfo = require('../../common/err_info.js');
var md5 = appUtils.md5;
var fout = appUtils.formatOutput;
var ferror = appUtils.formatError;


function DaoImpl() {
	var self = this instanceof DaoImpl ? this : new DaoImpl();
	return self;
}

module.exports = new DaoImpl();
var theProto = DaoImpl.prototype;

/*
{
	"email": "aa@bb.com",
	"phone": "13144025450",
	"name": "wzq",
	"password": "abc123",
	"rps": [{
		"orgnization": "root",
		"roles": "super_admin"
	}]
}
*/
theProto.create = function(json) {
	debug('create ', json);
	// TODO check
	var item = new model({
		email: json.email,
		name: json.name,
		password: md5(json.password),
		phone: json.phone,
		rps: json.rps,
		isSuperAdmin: json.isSuperAdmin
	});

	return item.save()
		.then(function(result) {
			return fout(result);
		}, function(err) {
			if (err.code === 11000) {
				return ferror(__errDuplicate(err.errmsg));
			}
			return ferror(err.code, 'mongodb error: ' + err.errmsg);
		});
};

// handle error info
function __errDuplicate(errmsg) {
	var ptnEmail = /email/g;
	var ptnName = /name/g;
	var ptnPhone = /phone/g;
	if (ptnEmail.exec(errmsg)) {
		return errInfo.E2005;
	}
	if (ptnName.exec(errmsg)) {
		return errInfo.E2004;
	}
	if (ptnPhone.exec(errmsg)) {
		return errInfo.E2006;
	}
}

theProto.update = function(json) {} // TODO
theProto.read = function(json) {
	// debug('read user');
	var query = json;
	return model.find(query, __defaultProject).exec()

	.then(function(result) {
		if (result.length === 0) return ferror(errInfo.E2000);
		return fout(result);
	}, function(err) {
		debug('dao user err:', err);
		return ferror(errInfo.E1000, 'mongodb error: ' + err.errmsg);
	});
};


theProto.readById = function(id) {
	// debug('read user');
	return model.findOne({
		_id: id
	}, __defaultProject).exec()

	.then(function(result) {
		if (result.length === 0) return ferror(errInfo.E2000);
		return fout(result);
	}, function(err) {
		debug('dao user err:', err);
		return ferror(errInfo.E1000, 'mongodb error: ' + err.errmsg);
	});
};

theProto.verify = function(json) {
	debug('verify user');
	var query = json;
	return model.find(query, __defaultProject).exec()

	// 
	.then(function(result) {
		if (result.length === 0) return ferror(errInfo.E2001);
		return fout(result[0]);
	}, function(err) {
		return ferror(err.code, 'mongodb error: ' + err.errmsg);
	})

	.then(function(result) {
		return result;
	});
};

/**
 * json email or name or phone
 */
theProto.remove = function(json) {
	debug('remove user');
	var query = json;
	return model.findOne(query).remove().exec()

	.then(function(result) {
		if (result.result.n) return fout(result.result); // have doc
		return ferror(errInfo.E2000); // doc not found
	}, function(err) {
		return ferror(err.code, 'mongodb error: ' + err.errmsg);
	});
};


var __defaultProject = {
	// _id: 0,
	__v: 0,
	password: 0
};